package com.itao.ds.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.itao.ds.bean.User;
import com.itao.ds.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author sjt
 * @CreateTime 2025/9/28 10:01
 */
@AllArgsConstructor
@RequestMapping("/user")
@RestController
public class UserController {

    private UserService userService;
    private DynamicRoutingDataSource dynamicRoutingDataSource;

    @PostMapping("/save")
    public String save(@RequestBody User user) {
        userService.saveUser(user);
        return "保存成功";
    }

    @PostMapping("/get/{id}")
    public User save(@PathVariable Long id) {
        return userService.getUser(id);
    }

    @PostMapping("/addDs")
    public String save() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        dataSource.setUrl("jdbc:mysql://localhost:3306/dynamic_db2?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setInitialSize(10);
        dataSource.setMaxActive(20);
        dynamicRoutingDataSource.addDataSource("dynamic", dataSource);
        return "添加数据源成功";
    }
}
